package com.glsc.ngateway.platform.controller.it;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.common.api.common.enums.DictConstant;
import com.glsc.ngateway.common.api.common.enums.PageConstant;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.*;
import com.glsc.ngateway.common.api.platform.dto.it.ParamOneKeySearchDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.platform.service.it.DomianOpearationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/itDomainOperation")
@Api(value = "域名维护", tags = {"域名维护"})
public class DomianOperationController {
    private static final Logger logger = LoggerFactory.getLogger(DomianOperationController.class);
    @Autowired
    private DomianOpearationService domianOpearationService;

    @RequestMapping(method = RequestMethod.POST, value = "domain/search")
    @ApiOperation(value = "domain/search", notes = "domain/search")
    public PlatformResponse<Page<ItDomain>> domainList(@RequestBody ParamOneKeySearchDto params) {

        if (params.getPageNo() == null) {
            params.setPageNo(PageConstant.PageNo);
        }
        if (params.getPageSize() == null) {
            params.setPageSize(PageConstant.PageSize);
        }
        Page<ItDomain> result = domianOpearationService.searchByKey(params.getKey(),params.getPageSize(),params.getPageNo());
        return PlatformResponse.successData(result);
    }

    @RequestMapping(method = RequestMethod.POST, value = "domain/listAll")
    @ApiOperation(value = "domain/listAll", notes = "domain/listAll")
    public PlatformResponse<List<ItDomain>> domainListAll() {
        List<ItDomain> result = domianOpearationService.domainListAll();
        return PlatformResponse.successData(result);
    }

    /*
     * 提交或修改项目
     */
    @RequestMapping(method = RequestMethod.POST, value = "domain/addOrUpdate")
    @ApiOperation(value = "domain/addOrUpdate", notes = "domain/addOrUpdate")
    public PlatformResponse<Void> addOrUpdateVirtual(@RequestBody ItDomain bean) {
        bean.setIsDelete(DictConstant.NO_DELETED);
        domianOpearationService.addOrUpdate(bean);
        return PlatformResponse.successMsg("创建或修改成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "domain/delete")
    @ApiOperation(value = "domain/delete", notes = "domain/delete")
    public PlatformResponse<Void> deleteVirtualDevice(@RequestBody String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return PlatformResponse.failedMsg("delete接口，id为空。");
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(id);
        String idJson = jsonObject.getString("id");
        if (StringUtils.isNotEmpty(idJson)) {
            domianOpearationService.deleteById(idJson);
        }
        return PlatformResponse.successMsg("删除成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "itsubDomain/search")
    @ApiOperation(value = "itsubDomain/search", notes = "itsubDomain/search")
    public PlatformResponse<Page<ItDomainManageWithChild>> domainManageList(@RequestBody ParamOneKeySearchDto params) {

        if (params.getPageNo() == null) {
            params.setPageNo(PageConstant.PageNo);
        }
        if (params.getPageSize() == null) {
            params.setPageSize(PageConstant.PageSize);
        }
        Page<ItDomainManageWithChild> result = domianOpearationService.searchDomainManageListByKey(params.getKey(),params.getPageSize(),params.getPageNo());
        return PlatformResponse.successData(result);
    }

    /*
     * 提交或修改项目
     */
    @RequestMapping(method = RequestMethod.POST, value = "itsubDomain/addOrUpdate")
    @ApiOperation(value = "itsubDomain/addOrUpdate", notes = "itsubDomain/addOrUpdate")
    public PlatformResponse<Void> domainManageListAddOrUpdate(@RequestBody ItDomainManage bean) {
        bean.setIsDelete(DictConstant.NO_DELETED);
        domianOpearationService.addOrUpdate(bean);
        return PlatformResponse.successMsg("创建或修改成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "itsubDomain/delete")
    @ApiOperation(value = "itsubDomain/delete", notes = "itsubDomain/delete")
    public PlatformResponse<Void> domainManageListDelete(@RequestBody String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return PlatformResponse.failedMsg("deletedomainManageListDelete接口，id为空。");
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(id);
        String idJson = jsonObject.getString("id");
        if (StringUtils.isNotEmpty(idJson)) {
            domianOpearationService.deleteDomainManageListDeleteById(idJson);
        }
        return PlatformResponse.successMsg("删除成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "officeDomain/search")
    @ApiOperation(value = "officeDomain/search", notes = "officeDomain/search")
    public PlatformResponse<Page<ItDomainOffice>> officeDomainList(@RequestBody ParamOneKeySearchDto params) {

        if (params.getPageNo() == null) {
            params.setPageNo(PageConstant.PageNo);
        }
        if (params.getPageSize() == null) {
            params.setPageSize(PageConstant.PageSize);
        }
        Page<ItDomainOffice> result = domianOpearationService.searchOfficeDomainListByKey(params.getKey(),params.getPageSize(),params.getPageNo());
        return PlatformResponse.successData(result);
    }

    /*
     * 提交或修改项目
     */
    @RequestMapping(method = RequestMethod.POST, value = "officeDomain/addOrUpdate")
    @ApiOperation(value = "officeDomain/addOrUpdate", notes = "officeDomain/addOrUpdate")
    public PlatformResponse<Void> officeDomainListAddOrUpdate(@RequestBody ItDomainOffice bean) {
        bean.setIsDelete(DictConstant.NO_DELETED);
        domianOpearationService.addOrUpdate(bean);
        return PlatformResponse.successMsg("创建或修改成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "officeDomain/delete")
    @ApiOperation(value = "officeDomain/delete", notes = "officeDomain/delete")
    public PlatformResponse<Void> officeDomainListDelete(@RequestBody String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return PlatformResponse.failedMsg("officeDomainListDelete接口，id为空。");
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(id);
        String idJson = jsonObject.getString("id");
        if (StringUtils.isNotEmpty(idJson)) {
            domianOpearationService.officeDomainListDeleteById(idJson);
        }
        return PlatformResponse.successMsg("删除成功");
    }


    @RequestMapping(method = RequestMethod.POST, value = "domainPort/search")
    @ApiOperation(value = "domainPort/search", notes = "domainPort/search")
    public PlatformResponse<Page<ItDomainPort>> domainPortList(@RequestBody ParamOneKeySearchDto params) {

        if (params.getPageNo() == null) {
            params.setPageNo(PageConstant.PageNo);
        }
        if (params.getPageSize() == null) {
            params.setPageSize(PageConstant.PageSize);
        }
        Page<ItDomainPort> result = domianOpearationService.searchDomainPortListByKey(params.getKey(),params.getPageSize(),params.getPageNo());
        return PlatformResponse.successData(result);
    }

    /*
     * 提交或修改项目
     */
    @RequestMapping(method = RequestMethod.POST, value = "domainPort/addOrUpdate")
    @ApiOperation(value = "domainPort/addOrUpdate", notes = "domainPort/addOrUpdate")
    public PlatformResponse<Void> domainPortListAddOrUpdate(@RequestBody ItDomainPort bean) {
        bean.setIsDelete(DictConstant.NO_DELETED);
        domianOpearationService.addOrUpdate(bean);
        return PlatformResponse.successMsg("创建或修改成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "domainPort/delete")
    @ApiOperation(value = "domainPort/delete", notes = "domainPort/delete")
    public PlatformResponse<Void> domainPortListDelete(@RequestBody String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return PlatformResponse.failedMsg("officeDomainListDelete接口，id为空。");
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(id);
        String idJson = jsonObject.getString("id");
        if (StringUtils.isNotEmpty(idJson)) {
            domianOpearationService.domainPortListDeleteById(idJson);
        }
        return PlatformResponse.successMsg("删除成功");
    }


    @RequestMapping(method = RequestMethod.POST, value = "domainPort/listAll")
    @ApiOperation(value = "domainPort/listAll", notes = "domainPort/listAll")
    public PlatformResponse<List<ItDomainPort>> domainPortListAll() {
        List<ItDomainPort> result = domianOpearationService.domainPortListAll();
        return PlatformResponse.successData(result);
    }

    @RequestMapping(method = RequestMethod.POST, value = "domainLog/search")
    @ApiOperation(value = "domainLog/search", notes = "domainLog/search")
    public PlatformResponse<Page<ItDomainLog>> domainLogList(@RequestBody ParamOneKeySearchDto params) {

        if (params.getPageNo() == null) {
            params.setPageNo(PageConstant.PageNo);
        }
        if (params.getPageSize() == null) {
            params.setPageSize(PageConstant.PageSize);
        }
        Page<ItDomainLog> result = domianOpearationService.searchDomainLogListByKey(params.getKey(),params.getPageSize(),params.getPageNo());
        return PlatformResponse.successData(result);
    }
    /*
     * 提交或修改项目
     */
    @RequestMapping(method = RequestMethod.POST, value = "domainLog/addOrUpdate")
    @ApiOperation(value = "domainLog/addOrUpdate", notes = "domainLog/addOrUpdate")
    public PlatformResponse<Void> domainLogListAddOrUpdate(@RequestBody ItDomainLog bean) {
        bean.setIsDelete(DictConstant.NO_DELETED);
        domianOpearationService.addOrUpdate(bean);
        return PlatformResponse.successMsg("创建或修改成功");
    }

    @RequestMapping(method = RequestMethod.POST, value = "domainLog/delete")
    @ApiOperation(value = "domainLog/delete", notes = "domainLog/delete")
    public PlatformResponse<Void> domainLogListDelete(@RequestBody String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            return PlatformResponse.failedMsg("officeDomainListDelete接口，id为空。");
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(id);
        String idJson = jsonObject.getString("id");
        if (StringUtils.isNotEmpty(idJson)) {
            domianOpearationService.domainLogListDeleteById(idJson);
        }
        return PlatformResponse.successMsg("删除成功");
    }


    @RequestMapping(method = RequestMethod.POST, value = "domainLog/listAll")
    @ApiOperation(value = "domainLog/listAll", notes = "domainLog/listAll")
    public PlatformResponse<List<ItDomainLog>> domainLogListAll() {
        List<ItDomainLog> result = domianOpearationService.domainLogListAll();
        return PlatformResponse.successData(result);
    }



    @RequestMapping(method = RequestMethod.POST, value = "truncateWideIpNetworkMap")
    @ApiOperation(value = "truncateWideIpNetworkMap", notes = "truncateWideIpNetworkMap")
    public PlatformResponse<?> truncateWideIpNetworkMap() {
        domianOpearationService.truncateWideIpNetworkMap();
        List<ItDomainWideIpNetworkMap> checkTruncate = domianOpearationService.findAllList();
        return checkTruncate.isEmpty() ? PlatformResponse.success() : PlatformResponse.failedMsg("清空失败");
    }

    @RequestMapping(method = RequestMethod.POST, value = "syncWideIpNetworkMap")
    @ApiOperation(value = "syncWideIpNetworkMap", notes = "syncWideIpNetworkMap")
    public PlatformResponse<?> syncWideIpNetworkMap(@RequestBody ItDomainWideIpNetworkMap itDomainWideIpNetworkMap) {

        domianOpearationService.saveWideIpNetworkMap(itDomainWideIpNetworkMap);
        return PlatformResponse.success();
    }

}
